#!/usr/bin/python
# vi: et sw=2
#
# pulse_control.py
# Copyright (C) 2009  Harry Karvonen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Author: Harry Karvonen <harry.karvonen@gmail.com>
#

from GnomePulseAppletInterface import *
import getopt, sys

clients = dict()
streams = dict()
sinks = dict()

def sink_input_append(sink):
  if sink.client_id in clients.keys():
    sink.client = clients[sink.client_id]

  streams[sink.index] = sink

def sink_append(sink):
  sinks[sink.index] = sink

def client_append(client):
  clients[client.index] = client

def action_complete():
  pass

def usage():
  print """
Usage: pulse-control.py <option1> <option2> ... <optionn>

Options:
        --help          Show this help
        --sinks         List sinks
        --streams       List streams (sink inputs)
        --clients       List clients
        --toggle-mute=<id1,id2,...,idn>
                        Toggle mute by stream or sink id
"""


def print_sinks(sinks):
  print "Sinks:"
  for a in sinks.keys():
    print sinks[a]

def print_streams(streams):
  print "Streams:"
  for a in streams.keys():
    print streams[a]

def print_clients(clients):
  print "Clients:"
  for a in clients.keys():
    print clients[a]

def print_separator():
  print 80 * "-"

def stream_toggle_mute(id):
  sink = None

  if id in sinks.keys():
    sink = sinks[id]

  elif id in streams.keys():
    sink = streams[id]

  else:
    print "Can't find sink by id (" + str(id) + ")"
    return

  if sink.mute == 0:
    sink.muteStream(pulse)
  else:
    sink.unmuteStream(pulse)

###########################3
##
##  MAIN
##

# Connect
pulse = PulseObj2("pulse_contorl.py")

# Callbacks
pulse.cb_sink_input_append = sink_input_append
pulse.cb_sink_append = sink_append
pulse.cb_client_append = client_append
pulse.cb_action_complete = action_complete

pulse.pulse_client_list()
pulse.pulse_sink_list()
pulse.pulse_sink_input_list()

try:
  opts, args = getopt.getopt(sys.argv[1:],
               "ho:",
               ["help", "sinks", "streams", "clients", "toggle-mute="]
  )
except getopt.GetoptError, err:
  # print help information and exit:
  print str(err) # will print something like "option -a not recognized"
  usage()
  sys.exit(2)

ln = False

if len(opts) == 0:
  usage()
  sys.exit()

for o, a in opts:
  if o in ("-h", "--help"):
    usage()
    sys.exit()
  elif o in ("--toggle-mute"):
    for p in a.split(","):
      try:
        stream_toggle_mute(int(p))
      except TypeError, err:
        print "Can't convert '" + p + "' to int"

  elif o in ("--sinks"):
    if ln:
      print_separator()
    print_sinks(sinks)
    ln = True
  elif o in ("--streams"):
    if ln:
      print_separator()
    print_streams(streams)
    ln = True
  elif o in ("--clients"):
    if ln:
      print_separator()
    print_clients(clients)
    ln = True
  else:
    assert False, "unhandled option"
